package com.lesserpand.rabbitmq.quartz.controller;

import com.lesserpand.rabbitmq.quartz.entity.dto.JobAndTriggerDTO;
import com.lesserpand.rabbitmq.quartz.job.HelloJob;
import com.lesserpand.rabbitmq.quartz.service.JobAndTriggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bailuo
 * @version 2.0
 * @Description TODO
 * @Date Created in 2019/1/19 17:08
 */
@RestController
@RequestMapping("/quartz")
public class JobAndTriggerController {
    @Autowired
    private JobAndTriggerService jobAndTriggerService;

    @GetMapping("/page")
    public List<JobAndTriggerDTO> page() {
        return jobAndTriggerService.getPageJob();
    }

    @PostMapping(value = "/job", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String addJob() {
        HelloJob helloJob = new HelloJob();
        return jobAndTriggerService.addJob(helloJob, "helloJob", "jobGroup1", helloJob.getJobDataMap(), "*/30 * * * * ?");
    }

    @PutMapping(value = "/job", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateJob() {
        HelloJob helloJob = new HelloJob();
        return jobAndTriggerService.addJob(helloJob, "helloJob", "jobGroup1", helloJob.getJobDataMap(), "0 */2 * * * ?");
    }

    @DeleteMapping(value = "/job")
    public void deleteJob() {
        jobAndTriggerService.deleteJob("helloJob", "jobGroup1");
    }

    @PutMapping(value = "/pause")
    public void pauseJob() {
        jobAndTriggerService.pauseJob("helloJob", "jobGroup1");
    }

    @PutMapping(value = "/resume")
    public void resumeJob() {
        jobAndTriggerService.resumejob("helloJob", "jobGroup1");
    }
}
